import Vue from "vue";
import VueRouter from "vue-router";
import home from "./modules/home.js";
import news from "./modules/news.js";
import goods from "./modules/goods.js";
import goodss from "./modules/goodss";
import parts from "./modules/parts";
import notfound from "./modules/404";
import me from "./modules/me.js";
import login from "./modules/login.js";
import register from "./modules/register.js";
import { Toast, MessageBox } from "mint-ui";

Vue.use(VueRouter);

const routes = [
  ...home,
  ...news,
  ...goods,
  ...goodss,
  ...parts,
  ...notfound,
  ...me,
  ...login,
  ...register,
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (!to.meta.auth && !sessionStorage.token) {
    // if (!confirm("您还未登录,点击确定跳转至登录页面")) return;
    MessageBox({
      title: "提示",
      message: "您还未登录，点击确定跳转至登录页面",
      showCancelButton: true,
    }).then((res) => {
      if (res == "confirm") {
        next("/login");
        Toast({
          message: "跳转成功",
          duration: 1500,
        });
      }
    });
  } else {
    next();
  }
});

export default router;
